﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Produce
{
    public partial class 日进度数据锁定 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Date1.Text = DateTime.Today.AddDays(-1).ToString("yyyy年M月d日");
            }
        }


        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Lock_Click(object sender, EventArgs e)
        {
            DateTime d;
            if(!DateTime.TryParse(this.Date1.Text,out d))
            {
                this.Message.Text="·日期输入有误";
                return;
            }
            string ds=d.ToString();
            if (this.FenChang.Text == "0")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度 set 锁定=1 where 日期='"+ds+"'"
                    +"update 生产进度3 set 锁定=1 where 日期='"+ds+"'"
                    +"update 生产进度4 set 锁定=1 where 日期='"+ds+"'";

            }
            else if (this.FenChang.Text == "1" || this.FenChang.Text == "2" || this.FenChang.Text == "5")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度 set 锁定=1 where 日期='"+ds+"' and 分厂ID="+this.FenChang.Text;
            }
            else if (this.FenChang.Text == "3" || this.FenChang.Text == "4")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度"+this.FenChang.Text+" set 锁定=1 where 日期='"+ds+"'";
            }
            else
            {
                return;
            }
            if (this.SqlDataSource1.Update() > 0)
            {
                this.Message.Text = "·锁定成功";
            }
            else
            {
                this.Message.Text = "·锁定失败，分厂可能没有录入日进度数据";
            }

        }

        protected void Button_Unlock_Click(object sender, EventArgs e)
        {
            DateTime d;
            if(!DateTime.TryParse(this.Date1.Text,out d))
            {
                this.Message.Text="·日期输入有误";
                return;
            }
            string ds=d.ToString();
            if (this.FenChang.Text == "0")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度 set 锁定=0 where 日期='"+ds+"'"
                    +"update 生产进度3 set 锁定=0 where 日期='"+ds+"'"
                    +"update 生产进度4 set 锁定=0 where 日期='"+ds+"'";

            }
            else if (this.FenChang.Text == "1" || this.FenChang.Text == "2" || this.FenChang.Text == "5")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度 set 锁定=0 where 日期='"+ds+"' and 分厂ID="+this.FenChang.Text;
            }
            else if (this.FenChang.Text == "3" || this.FenChang.Text == "4")
            {
                this.SqlDataSource1.UpdateCommand = "update 生产进度"+this.FenChang.Text+" set 锁定=0 where 日期='"+ds+"'";
            }
            else
            {
                return;
            }
            if (this.SqlDataSource1.Update() > 0)
            {
                this.Message.Text = "·解锁成功";
            }
            else
            {
                this.Message.Text = "·解锁失败，分厂可能没有录入日进度数据";
            }
        }
    }
}
